package com.example.weibo_app.service.impl;

import com.example.weibo_app.domain.Article;
import com.example.weibo_app.mapper.ArticleMapper;
import com.example.weibo_app.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public int addArticle(Article article) {
        return articleMapper.insertArticle(article);
    }

    @Override
    public Article getArticleById(Integer id) {
        if (articleMapper.selectArticleById(id) == null){
            throw new RuntimeException("文章ID不存在");
        }
        return articleMapper.selectArticleById(id);
    }

    @Override
    public List<Article> getAllArticles() {
        return articleMapper.selectAllArticles();
    }

    @Override
    public List<Article> getArticlesByUserId(Integer userId) {
        return articleMapper.selectArticlesByUserId(userId);
    }

    @Override
    public int updateArticleContent(Integer id, String content) {
        int rowsAffected = articleMapper.updateArticleContent(id, content);
        if (rowsAffected == 0) {
            throw new RuntimeException("更新文章失败，文章ID不存在");
        }
        return rowsAffected;
    }

    @Override
    public int deleteArticleById(Integer id) {
        int rowsAffected = articleMapper.deleteArticleById(id);
        if (rowsAffected == 0) {
            throw new RuntimeException("删除文章失败，文章ID不存在");
        }
        return rowsAffected;
    }
}
